import PubSub from "pubsub-js"
import type { BannerModel, RecommedModel, PlaylistModel, TopListModel, TracksModel } from "../../request/home"
import { reqBannerList, reqRecommedList, reqRankList, reqTopList } from "../../request/home"
import { localNum } from "../../utils/util"

let srcRankList: {
  [key: string]: { data: TracksModel[]; id: number; type: "rank" }
} = {}

let srcNewSongList: { id?: number; data?: TopListModel[]; type: "news" } = { type: "news" }

Page<DataModel, CustomOptionModel>({
  data: {
    banners: [],
    recommeds: [],
    ranks: [],
    newSongs: [],
  },

  onLoad() {
    getBannerList.bind(this)()
    getRecommedList.bind(this)()
    getRankList.bind(this)()
    getTopList.bind(this)()
  },

  onShow() {
    this.getTabBar().setData({
      currentPage: "index",
    })
  },

  setSong(e) {
    let list: any
    let { type, name, id } = e.currentTarget.dataset //这个id是songId
    if (type === "rank") list = srcRankList[name]
    else if (type === "news") list = srcNewSongList

    PubSub.publish("playSong", {songId: id})
    PubSub.publish("setPlaylist", list)
  },

  //获取点击的推荐歌单id
  getPlaylistId(e) {
    wx.navigateTo({
      url: "/pages/playlist/playlist?id=" + e.detail,
    })
  },
})

//* ---------------获取banner轮播--------------------
async function getBannerList(this: Instance) {
  const res = await reqBannerList()
  this.setData({ banners: res.banners })
}

//* ---------------获取推荐歌单--------------------
async function getRecommedList(this: Instance) {
  const res = await reqRecommedList()
  res.result.forEach(item => (item.localPlayCount = localNum(item.playCount, item.playCount > 99999999 ? 1 : 0)))
  this.setData({ recommeds: res.result })
}

//* -------获取排行榜，每次只能获取单独一个分类，需要5个就发5次-----------
function getRankList(this: Instance) {
  ;[6, 3, 1, 5, 8].forEach(async type => {
    const { playlist } = await reqRankList(type)
    //根据榜单名字记录榜单的源数据
    srcRankList[playlist.name] = {
      id: playlist.id,
      data: [...playlist.tracks],
      type: "rank",
    }

    playlist.tracks.length = 3 //只要3个
    const ranks = [...this.data.ranks, playlist]
    // this.data.ranks.push(res.playlist)
    this.setData({ ranks })
  })
}

//* ------------获取欧美新歌速递-----------------------
async function getTopList(this: Instance) {
  const res = await reqTopList()

  srcNewSongList.id = 1111 //这个接口直接返回数组,没有id,只能自己写一个标识
  srcNewSongList.data = res.data

  //只渲染12条
  res.data.length >= 12 && (res.data.length = 12)
  //把这12条分成4分的二维数组
  const arr = [
    [...res.data.slice(0, 3)],
    [...res.data.slice(3, 6)],
    [...res.data.slice(6, 9)],
    [...res.data.slice(9, 12)],
  ]
  this.setData({ newSongs: arr })
}

interface DataModel {
  banners: BannerModel[]
  recommeds: RecommedModel[]
  ranks: PlaylistModel[]
  newSongs: TopListModel[][]
}

interface CustomOptionModel {
  setSong: (e: WechatMiniprogram.Touch) => void
  getPlaylistId: (e: WechatMiniprogram.CustomEvent) => void
}

type Instance = WechatMiniprogram.Page.Instance<DataModel, CustomOptionModel>
